package com.djzhou.gmms.base.action;

import java.lang.reflect.Member;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import ognl.DefaultTypeConverter;

/**
 * 重写struts2 Date转String类型转换器
 * @author DJZHOU
 *
 */
public class DateConverter extends DefaultTypeConverter {

	@Override
	public Object convertValue(Map context, Object target, Member member,
			String propertyName, Object value, Class toType) {
		if (toType == Date.class) {
			try {
				String dataStr = ((String[]) value)[0];
				SimpleDateFormat f=null;
				if(dataStr.length()>17){
					f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				}else if(dataStr.length()>11){
					f = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				}else {
					f = new SimpleDateFormat("yyyy-MM-dd");
				}
				return f.parse(dataStr);
			} catch (ParseException e) {

			}
		} else if (toType == String.class) {
			String dataStr = ((Date) value).toString();
			return dataStr;
		}
		return null;
	}
}
